package gbench.sandbox.record.xls;

import gbench.common.fs.FileSystem;
import gbench.common.fs.XlsFile.*;
import gbench.common.tree.LittleTree.IRecord;
import gbench.common.tree.LittleTree.SQL;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;

public class JunitShihao {
    
    /**
     * SQL的创建脚本
     * @param path EXCEL 源文件路径
     * @param tableName 表名
     * @param shtId 表单id 从0开始,null 默认为1
     * @param mode 1:drop tabe is exists, 2:create table,null 默认为1
     * @return SQL 脚本
     */
    public String getSqlScript(final String path,Integer shtId,final String tableName,final Integer mode) {
        var f = new SimpleExcel();
        f.loadXlsx(FileSystem.stream(path));
        var xx = f.autoDetect(shtId==null?0:shtId);
        xx.setHeader("Symbol,Meanings");
        var last = new AtomicReference<IRecord>();
        var buffer = new StringBuffer();
        xx.mapByRow(IRecord::REC).forEach(e->{
            buffer.append(new SQL(tableName,e).insert());
            buffer.append("\n");
            last.set(e);
        });
        f.close();
        
        return new SQL(tableName,last.get()).createTable().get(mode==null?1:mode)+"\n"+
            buffer.toString();
    }
    
    @Test
    public void foo() {
        final var path = FileSystem.path("shihao.xlsm",this.getClass());
        final var tableName = "T_SHIHAO";
        final var sqlScript = this.getSqlScript(path,null, tableName,null);
        System.out.println(sqlScript);
    }

}
